package com.elong.businesstravel.modules.home.ticket.search;

import static android.widget.Toast.LENGTH_SHORT;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.timessquare.CalendarCellView;
import com.android.support.jhf.timessquare.CalendarPickerView;
import com.android.support.jhf.timessquare.MonthCellDescriptor;
import com.android.support.jhf.timessquare.CalendarPickerView.OnDateSelectedListener;
import com.android.support.jhf.timessquare.CalendarPickerView.OnInvalidDateSelectedListener;
import com.android.support.jhf.timessquare.CalendarPickerView.SelectionMode;
import com.android.support.jhf.utils.DateUtils;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.Constant;
import com.elong.businesstravel.base.baseactivity.BaseActivity;
import com.elong.businesstravel.base.utils.SaveUserOperate;
import com.elong.businesstravel.base.view.TitleNavBarView;

/**
 * 火车出发日期
 * 
 * @author lenovo
 * 
 */
public class TrainStartingDateActivity extends BaseActivity {

	private CalendarPickerView calendar;

	private Calendar minCalendar;
	private Calendar maxYear;
	
	private Date mSelecteDate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hotel_destione_date);

		init();
	}

	@Override
	protected void init() {
		
		mSelecteDate = (Date) getIntent().getSerializableExtra("selectedDate");

		minCalendar = Calendar.getInstance();
		minCalendar.setTimeInMillis(System.currentTimeMillis());

		maxYear = Calendar.getInstance();
		maxYear.setTimeInMillis(System.currentTimeMillis());
		maxYear.add(Calendar.DAY_OF_YEAR, Constant.TRAIN_TICKET_DAY_COUNT);

		calendar = (CalendarPickerView) findViewById(R.id.calendar_view);

		String valueString1 = "" + "\n入住";
		SpannableString spannableString1 = new SpannableString(valueString1);

		String valueString2 = "" + "\n离店";
		SpannableString spannableString2 = new SpannableString(valueString2);

		calendar.setFirstLastValue(spannableString1, spannableString2);

		Date currentDate = new Date(System.currentTimeMillis());
		Date saveDate = SaveUserOperate.getTrainStartingDate(mContext);
		if(null != saveDate){
			currentDate = saveDate;
		}
		if(null != mSelecteDate){
			currentDate = mSelecteDate;
		}
		calendar.init(minCalendar.getTime(), maxYear.getTime()) //
				.inMode(SelectionMode.SINGLE) //
				.withSelectedDate(currentDate);

		setTitleNavBar();

		setListener();
	}
	
	@Override
	protected void refreshDateUI(String showDateUIString) {
		super.refreshDateUI(showDateUIString);
		init();
	}

	@Override
	protected void setListener() {
		calendar.setOnDateSelectedListener(new OnDateSelectedListener() {

			private MonthCellDescriptor lastMonthCellDescriptor = null;

			@Override
			public void onDateUnselected(Date date) {
			}

			@Override
			public void onDateSelected(Date date,
					MonthCellDescriptor monthCellDescriptor) {

//				Date selecteDate = calendar.getSelectedDate();
//				HandlerToastUI.getHandlerToastUI(mContext,selecteDate.toString());
				
				// 保存出发时间
				SaveUserOperate.setTrainStartingDate(mContext, date);
				
				String resultDate =  DateUtils.dateFormat(
						date.getTime(), "yyyy/MM/dd");
				Intent intent = new Intent();
				intent.putExtra("date", resultDate);
				setResult(RESULT_OK, intent);
				finish();
				

			}
		});
		calendar.setOnInvalidDateSelectedListener(new OnInvalidDateSelectedListener() {

			@Override
			public void onInvalidDateSelected(Date date) {
				// HandlerToastUI.getHandlerToastUI(mContext, "当前日期不能选择");
			}
		});
	}

	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("选择出发日期");
		titleNavBarView.setTitleBackground(R.color.bg_title_dialog);
		titleNavBarView.setLeftButtonImageString(0, "取消", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				quit();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	private void quit() {
		finish();
		overridePendingTransition(R.anim.activity_alpha_fixed,
				R.anim.activity_top_bottom_exit_anim_translate);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			quit();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

}
